using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Soopah.Xna.Input;
using Microsoft.Xna.Framework.Content;
// TODO: replace this with the type you want to write out.


namespace XNADirectInput
{
    public class AnalogInputReader : ContentTypeReader<AnalogInput>
    {
        protected override AnalogInput Read(ContentReader input, AnalogInput existingInstance)
        {
            if (existingInstance == null)
                existingInstance = new AnalogInput();
            
            existingInstance.Index = input.ReadInt32();
            existingInstance.XAxis = input.ReadString();
            existingInstance.YAxis = input.ReadString();
            return existingInstance;
        }
    }

    public class ButtonSettingsReader : ContentTypeReader<ButtonSettings>
    {
        protected override ButtonSettings Read(ContentReader input, ButtonSettings existingInstance)
        {
            if (existingInstance == null)
                existingInstance = new ButtonSettings();

            existingInstance.Name = input.ReadString();
            existingInstance.AIndex = input.ReadInt32();
            existingInstance.BIndex = input.ReadInt32();
            existingInstance.XIndex = input.ReadInt32();
            existingInstance.YIndex = input.ReadInt32();
            existingInstance.LBIndex = input.ReadInt32();
            existingInstance.RBIndex = input.ReadInt32();
            existingInstance.BackIndex = input.ReadInt32();
            existingInstance.StartIndex = input.ReadInt32();
            existingInstance.LStickIndex = input.ReadInt32();
            existingInstance.RStickIndex = input.ReadInt32();
            existingInstance.HasLeftStick = input.ReadBoolean();
            existingInstance.HasRightStick = input.ReadBoolean();
            existingInstance.HasShoulders = input.ReadBoolean();
            existingInstance.Sticks = input.ReadObject<List<AnalogInput>>();

            return existingInstance;

        }
    }

    [ContentTypeWriter]
    public class AnalogInputWriter : ContentTypeWriter<AnalogInput>
    {
        protected override void Write(ContentWriter output, AnalogInput value)
        {
            output.Write(value.Index);
            output.Write(value.XAxis);
            output.Write(value.YAxis);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(AnalogInputReader).AssemblyQualifiedName;
        }
    }

    /// <summary>
    /// Writes ButtonSettings Files
    /// </summary>
    [ContentTypeWriter]
    public class ButtonSettingsWriter : ContentTypeWriter<ButtonSettings>
    {
        protected override void Write(ContentWriter output, ButtonSettings value)
        {
            output.Write(value.Name);
            output.Write(value.AIndex);
            output.Write(value.BIndex);
            output.Write(value.XIndex);
            output.Write(value.YIndex);
            output.Write(value.LBIndex);
            output.Write(value.RBIndex);
            output.Write(value.BackIndex);
            output.Write(value.StartIndex);
            output.Write(value.LStickIndex);
            output.Write(value.RStickIndex);
            output.Write(value.HasLeftStick);
            output.Write(value.HasRightStick);
            output.Write(value.HasShoulders);
            output.WriteObject(value.Sticks);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            // TODO: change this to the name of your ContentTypeReader
            // class which will be used to load this data.
            return typeof(ButtonSettingsReader).AssemblyQualifiedName;
        }
    }
}
